<!-- @format -->

<!-- 换单 -->
<template>
	<div class="page-container" style="min-height: 55rem;">
		<audio ref="error_audio">
			<source src="../../../../asset/error_audio.wav" type="audio/mpeg" />
			Your browser does not support the audio element.
		</audio>
		<el-form label-position="right" ref="formSearch" class="mg_t2" onsubmit="return false" :rules="rules" :model="{}">
			<el-form-item :label="$t('label_international_waybill')" prop="searchCode">
				<scan_input v-model.trim="searchCode" @enterInput="scanSkuCode" style="width:400px" class="form_input_default mg_l1" autofocus> </scan_input>
				<el-button class="mg_l1" @click="scanSkuCode" type="primary" style="width:110px"> {{ $t('label_scan') }}</el-button>
				<el-checkbox class="mg_1" v-model="autoPushOver" border @input="configChange">{{ $t('label_auto_confirm_change_order') }}</el-checkbox>
			</el-form-item>
		</el-form>

		<div class=" flex_col card_1" key="trackLinkTableDiv" style="height: 260px;margin-bottom: 20px; box-sizing: border-box;">
			<transition name="el-zoom-in-top" mode="out-in" :duration="200">
				<div v-if="trackSkuTableData.length > 0" class=" flex_col flex_cell">
					<ex_super_table :data="trackSkuTableData" v-bind="trackSkuTableOption" border autoFlex key="trackLinkTable"> </ex_super_table>
				</div>
				<div v-else class=" flex_center flex_cell">
					{{ $t('tip_scan_sku_only_code_first') }}
				</div>
			</transition>
			<div v-if="trackSkuTableData.length > 0" style="display: flex; justify-content: space-between;">
        <span>{{ trackSkuMsg }}</span>
				<el-button type="primary" v-show="scanStatus==='Y'&& !autoPushOver" @click="pushCurrentTrackDown">
					{{ $t('label_confirm_change_order') }}
				</el-button>
			</div>
		</div>

		<ex_super_table class="flex_auto" style="min-height: 350px;" v-bind="tableOption" :data="tableData" :autoFlex="true" :autoPage="false" :specialStyle="false">
		</ex_super_table>

    <common_print_dialog_url ref="common_print_dialog_url"/>
	</div>
</template>
<script>
import superTablePageFunction from '@/mixin/super-table-page-function';
import page_table_model_manage from '@/mixin/page_table_model_manage';

import common_print_dialog_url from '@/component/print/common_print_dialog_url'
export default {
	title: 'title_change_order',
	mixins: [superTablePageFunction, page_table_model_manage],
	components: {common_print_dialog_url},
	data() {
		return {
			// 自动确认换单
			autoPushOver: false,
			// 扫描code
			searchCode: '',
      // 是否扫描完成
      scanStatus:"N",
			rules: {
				searchCode: {
					required: true,
					trigger: 'blur',
					validator: (r, v, cb) => {
						if (_.isEmpty(this.searchCode)) {
							return cb(this.$tt('tip_can_no_empty'));
						}
						cb();
					},
				},
			},
			// 扫描后待确认订单
			trackSkuTableData: [],
			trackSkuTableOption: {
				model: [
					{ key: 'mainOrderCode', label: 'label_srm_api_request_order_no', 'min-width': 140 },
					{ key: 'beginExpressNumber', label: 'label_international_waybill', 'min-width': 140 },
					{ key: 'scanStatus', label: 'label_scan_status', 'min-width': 140,formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum") },
				],
			},
			// 已确认订单
			tableData: [],
			tableOption: {
				model: [
					{ key: 'mainOrderCode', label: 'label_srm_api_request_order_no', 'min-width': 140 },
					{ key: 'beginExpressNumberList', label: 'label_international_waybill', 'min-width': 240,formatter: (r, c, v, i) => v.join(',') },
					{ key: 'endExpressNumber', label: 'label_final_supplier', 'min-width': 140 },
					{ key: 'endExpressCode', label: 'label_final_order', 'min-width': 140 },
					{ label: 'label_call_courier', 'min-width': 140 ,formatter: (r, c, v, i) => this.$enumShow('Y', "YesOrNoEnum")},
					{
						type: 'operate',
						width: 120,
						operate: [
							{
								label: 'label_again_print_face_code',
								click: (row) => {
                  row.filePath=row.endLabelPath
                  this.$refs.common_print_dialog_url.show([row],{titleText:'label_print_face_code'})
                },
							},
						],
					},
				],
			},
		};
	},

	mounted() {
		this.autoPushOver = JSON.parse(localStorage.getItem('outbound>change_order>autoPushOver'));
	},
	methods: {
		//扫描sku
		scanSkuCode() {
			this.$refs.formSearch.validate((valid) => {
				if (valid) {
					console.log('单号：', this.searchCode);
					this.$ajax({
						url: '/wms-api/inbound/WmsHaStoreMerOrderRpc/queryByBeginExpressNumber.json',
						method: 'get',
						type: 'json',
						loading: true,
						data: { beginExpressNumber: this.searchCode },
						success: (res) => {
							if (res.data) {
                this.trackSkuTableData = res.data.orderDTOList
                this.trackSkuMsg = res.data.msg
                this.scanStatus = res.data.scanStatus

                this.tableData = []
                // 自动换单
                if(this.autoPushOver && this.scanStatus==='Y'){
                  this.pushCurrentTrackDown()
                }
							}
						}
					});
				} else {
					return false;
				}
			});
		},
		// 配置自动确认换单
		configChange() {
			localStorage.setItem('outbound>change_order>autoPushOver', this.autoPushOver);
		},
		// 确认换单
		pushCurrentTrackDown() {
      this.$ajax({
						url: '/wms-api/inbound/WmsHaStoreMerOrderRpc/changeExpressNumber.json',
						method: 'post',
						type: 'json',
						loading: true,
						data: { orderIds: this.trackSkuTableData.map(it=>it.id) },
						success: (res) => {
							if (res.data) {
                // 重置扫描区
                // this.trackSkuTableData = []
                // this.trackSkuMsg = ''
                // this.scanStatus = 'N'
                // 赋值
                this.tableData = [res.data]
							}
						}
					});
    },
    urlGetFun(){
      return { url: this.filePath, oss: true }
    }
	},
};
</script>
<style></style>

